import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

const path = require('path')

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, './')

  return {
    mode,
    server: {
      port: 4000,
      host: '0.0.0.0',
      // open: true,
      proxy: {
        '/api': {
          target: 'https://open.ahapocket.cn',
          changeOrigin: true,
        }
      },
      headers: {
        'Access-Control-Allow-Origin': '*',
      },
    },
    base: mode === 'development' ? './' : `${ env.VITE_APP_ROOT }`, // 生产环境要绝对路径，否则子应用无法找到
    build: {
      assetsDir: './'
    },
    resolve: { // 配置路径别名
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    css: { // css配置
      preprocessorOptions: {
        scss: { // 全局scss注入
          additionalData: `
            @use "@/styles/common.scss" as *;
          `
        }
      }
    },
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ]
  }
})
